home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1014 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.0 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: kanze@gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: sample auto_ptr template
  5. Date: 9 Apr 1996 14:53:47 GMT
  6. Organization: GABI Software, Sarl.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <KANZE.96Apr9115532@gabi.gabi-soft.fr>
  9. References: <009A04DA6A831C40.49800EAC@ittpub.nl> <4k0m72$gm1@jabba.lehman.com>
  10.     <bill-0504961003150001@bgibbons.vip.best.com> <4k4noe$igl@jabba.lehman.com>
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. X-Nntp-Posting-Host: gabi.gabi-soft.fr
  13. In-Reply-To: ajay@lehman.com's message of 06 Apr 96 05:35:59 GMT
  14. Content-Length: 3080
  15. X-Lines: 80
  16. Originator: clamage@taumet
  17.  
  18. In article <4k4noe$igl@jabba.lehman.com> ajay@lehman.com (Ajay Kamdar)
  19. writes:
  20.  
  21. |> In article <bill-0504961003150001@bgibbons.vip.best.com>,
  22. |> Bill Gibbons <bill@gibbons.org> wrote:
  23. |> >
  24. |> >Transfer of ownership is not the end goal - the end goal is
  25. |> >to make auto_ptr useful for the "resource acquisition is
  26. |> >initialization" idiom.  That is very painful without transfer
  27. |> >of ownership.
  28. |> >
  29. |> >In particular, if you want to do the resource acquisition in a
  30. |> >function called by the function which needs to hold the resource,
  31. |> >there is no good exception-safe way to pass the pointer from the
  32. |> >callee to the caller.
  33. |> >
  34. |> >You can get close (at some cost in clarity):
  35. |> >
  36. |>        [ snip... ]
  37.  
  38. |> It is not clear at all that the copy semantics of auto_ptr
  39. |> are essential for exception-safe transfer of resources.
  40. |> The same example coded as follows does not use
  41. |> the copy semantics of auto_ptr:
  42.  
  43.  
  44. |>     extern X* get_X();   // returns a resource acquired
  45. |>              // by the callee, to be deleted
  46. |>              // by the caller.
  47.  
  48. |>     void f() {
  49. |>         auto_ptr<X> ptr = get_X();
  50. |>             // resource allocated by get_X()
  51. |>         ...
  52. |>    }
  53.  
  54. |> And get_X() is not unnecessarily complicated either:
  55.  
  56. |>    X* get_X()
  57. |>    {
  58. |>       auto_ptr<X> p = new X;
  59.  
  60. |>       // ... stuff that could throw an exception
  61.  
  62. |>       // We got here. Means normal return.
  63. |>       return p.release();
  64. |>    }
  65.  
  66.  
  67. |> What's wrong with this? It doesn't require copy semantics
  68. |> for auto_ptr. Yet both the caller and the callee
  69. |> are exception safe and there is no loss of clarity.
  70.  
  71. As Bill pointed out in a previous incarnation of this thread, `get_X',
  72. as written above is *NOT* exception safe.  After the call to p.release,
  73. the compiler will still cause all of the destructors of any local
  74. variables (and any value parameters to the function) to be called.  If
  75. any of these destructors throw an exception, you've lost p.  For good.
  76.  
  77. Now, I don't generally think it a good idea to let an exception
  78. propagate out of a destructor.  But I don't think that a standard
  79. auto_ptr class should require this sort of additional rule in order to
  80. be useful.
  81.  
  82. Personally, I would have preferred to see auto_ptr set the pointer to
  83. NULL when ownership was transfered, even though this meant having a
  84. const parameter that wasn't.  But I consider the current version
  85. acceptable, and if it is the compromise that is needed to obtain
  86. consensus, I will go along with it.  (Although I wouldn't mind an
  87. additional function to indicate whether I was owner or not.)
  88.  
  89. BTW: although I do not, nor never have, worked for Taligent, my
  90. experience does parallel theirs.  So when comparing the number of people
  91. in favor of the idiom, you have to count some of those involved in the
  92. previous discussions, as well as those at Taligent.
  93. -- 
  94. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  95. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  96. Conseils en informatique industrielle --
  97.                             -- Beratung in industrieller Datenverarbeitung
  98.  
  99.  
  100. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  101. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  102. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  103. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  104. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  105.